package com.yygh.hosp.control.api;



import com.yygh.common.exception.YyghException;
import com.yygh.common.helper.HttpRequestHelper;
import com.yygh.common.result.Result;
import com.yygh.common.result.ResultCodeEnum;
import com.yygh.common.util.MD5;
import com.yygh.hosp.repository.DepartmentRepository;
import com.yygh.hosp.service.DepartmentService;
import com.yygh.hosp.service.HospitalService;
import com.yygh.hosp.service.HospitalSetService;
import com.yygh.hosp.service.ScheduleService;
import com.yygh.hosp.service.impl.HospitalSetServiceImpl;
import com.yygh.model.hosp.Department;
import com.yygh.model.hosp.Hospital;
import com.yygh.model.hosp.Schedule;
import com.yygh.vo.hosp.DepartmentQueryVo;
import com.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestMap);

        String sign = (String) switchMap.get("sign");
        String hoscode = (String) switchMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(signKey);
        if (signKeyMd5.equals(sign)) {
            String logoData = (String) switchMap.get("logoData");
            logoData.replaceAll(" ", "+");
            switchMap.put("logoData", logoData);
            hospitalService.save(switchMap);
            return Result.ok();
        } else {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestParameterMap);
        String hoscode = (String) switchMap.get("hoscode");
        String sign = (String) switchMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);

        if (sign.equals(encrypt)) {
            Hospital byHoscode = hospitalService.getByHoscode(hoscode);
            return Result.ok(byHoscode);
        } else {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    @PostMapping("saveDepartment")
    //科室添加
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestParameterMap);

        String hoscode = (String) switchMap.get("hoscode");
        String sign = (String) switchMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);

        if (sign.equals(encrypt)) {
            departmentService.save(switchMap);
            return Result.ok();
        } else {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //TODO 签名校验
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);
        if(true){
            DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
            departmentQueryVo.setHoscode(hoscode);
            //调用service方法
            Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
            System.out.println(pageModel);
            return Result.ok(pageModel);
        }
        return Result.fail();

    }

    @PostMapping("department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestParameterMap);

        String hoscode = (String) switchMap.get("hoscode");
        String depcode = (String) switchMap.get("depcode");

        String sign = (String) switchMap.get("sign");
        System.out.println(sign);
        String signKey = hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(signKey);
        System.out.println(encrypt);

        departmentService.remove(hoscode,depcode);

        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //TODO 签名校验

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 签名校验
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和排班编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        //TODO 签名校验

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

}
